package com.heima.ssm.dao;

import com.heima.ssm.pojo.Permission;
import com.heima.ssm.pojo.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Penguin
 * @create Date 2021/5/5  Time 19:15
 */
public interface IRoleDao {

    @Select("select * from role where id in (select roleId from users_role where userId = #{usersId})")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "roleName",column = "roleName"),
            @Result(property = "roleDesc",column = "roleDesc"),
            @Result(property = "permissions",column = "id",javaType = List.class,
                    many = @Many(select = "com.heima.ssm.dao.IPermissionDao.findPermissionByPerIdWhereFromRole_Permission"))
    })
    List<Role> findRoleByRoleIdWhereFromUsersRoleRelationship(int usersId);

    @Select("select * from role")
    List<Role> findAll();

    @Insert("insert into role(roleName,roleDesc) values(#{roleName},#{roleDesc})")
    void add(Role role);

    @Select("select * from role where id = #{roleId}")
    Role findById(int roleId);

    @Select("select * from permission where id not in (select permissionId from role_permission where roleId = #{roleId})")
    List<Permission> findOtherPermissionByRoleId(int roleId);

    @Insert("insert into role_permission(roleId,permissionId) values(#{roleId},#{permissionId})")
    void addPermissionToRole(@Param("roleId") int roleId, @Param("permissionId") int permissionId);
}
